/*
 *  Licensed to the Apache Software Foundation (ASF) under one
 *  or more contributor license agreements.  See the NOTICE file
 *  distributed with this work for additional information
 *  regarding copyright ownership.  The ASF licenses this file
 *  to you under the Apache License, Version 2.0 (the
 *  "License"); you may not use this file except in compliance
 *  with the License.  You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing,
 *  software distributed under the License is distributed on an
 *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 *  KIND, either express or implied.  See the License for the
 *  specific language governing permissions and limitations
 *  under the License.
 */
package org.apache.myfaces.test.webapp.api.runner;

import org.apache.myfaces.test.webapp.api.ResourceFinder;
import org.apache.myfaces.test.webapp.api.WebappTestConfigurator;
import org.apache.myfaces.test.webapp.api.tester.WebappTester;
import org.jboss.arquillian.junit.Arquillian;
import org.junit.runner.notification.RunNotifier;
import org.junit.runners.model.FrameworkMethod;
import org.junit.runners.model.InitializationError;
import org.junit.runners.model.Statement;

/**
 * @author Cosmin Martinconi, 05.08.2010
 */
public class WebappTestRunner extends Arquillian {

    private WebappTester webappTester;
    private WebappTestConfigurator wtConfig;

    public WebappTestRunner(Class<?> klass) throws InitializationError {
        super(klass);

        wtConfig = WebappTestConfigurator.getCurrentInstance();
        wtConfig.configure(klass);        

        webappTester = (WebappTester)ResourceFinder.getResource(ResourceFinder.WEBAPP_TESTER);
    }

    @Override
    public void run(final RunNotifier notifier) {

        super.run(notifier);

        if (wtConfig.isTestSuiteConfig() && !wtConfig.isInContainer()) {
            this.run(notifier);
        }
    }

    @Override
    protected Statement withBefores(final FrameworkMethod method, final Object target, final Statement originalStatement) {

        if (wtConfig.isInContainer()) {
            webappTester.setUpTestMethod();
        }

        return super.withBefores(method, target, originalStatement);
    }

    @Override
    protected Statement withAfters(final FrameworkMethod method, final Object target, final Statement originalStatement) {
        final Statement statementWithAfters = super.withAfters(method, target, originalStatement);
        return new Statement() {
            @Override
            public void evaluate() throws Throwable {
                statementWithAfters.evaluate();

                if (wtConfig.isInContainer()) {
                    webappTester.executeTestMethod();
                    webappTester.tearDownTestMethod();
                }
            }
        };
    }

    @Override
    protected Statement methodInvoker(final FrameworkMethod method, final Object test) {
        if (getTestClass().getJavaClass().isInstance(test)) {
            wtConfig.injectAssertableProxies(test);
        }

        return super.methodInvoker(method, test);
    }

}